package com.example.memolaga.accounts;

import android.util.Log;

import com.example.memolaga.Constants;
import com.example.memolaga.Functions;
import com.example.memolaga.password.FunctionsPass;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

public class AccountsManager {
	
    private ArrayList<AccountsMaster> accSaved;
	
	private static AccountsManager instance = null;
	
	private AccountsManager(){this.accSaved = new ArrayList<AccountsMaster>();}
	
	public static AccountsManager getInstance(){if (instance==null) instance=new AccountsManager(); return instance;}
	
	public int addAccFromFile(String path)
    {
        if (!(new File(path).exists())) return 0;
        int size = this.accSaved.size();
        loadData(Functions.getStringOfFile(path));
        return this.accSaved.size()-size;
    }

	public int getNAccounts()
	{
		return this.accSaved.size();
	}

    public ArrayList<AccountsMaster> getAccSaved()
    {
        return this.accSaved;
    }

    public AccountsMaster getAccount(String name)
    {
        if (this.accSaved!=null)
            for (int i=0; i<this.accSaved.size();i++)
                if (name.equals(this.accSaved.get(i).getName()))
                    return this.accSaved.get(i);
        return null;
    }

    public AccountsMaster getAccIndex(int i)
    {
        if (i<this.accSaved.size())
            return this.accSaved.get(i);
        else
            return null;
    }

    public void accAdd(String name, String server, String path, String userName,
                       String pass, String provider, String providerWebSite,
                       String providerPhone, String note, String sKey)
    {
        this.aAdd(name, server, path, userName, pass, provider, providerWebSite, providerPhone,
                  note, sKey);
    }

    public void accAdd(ArrayList<AccountsMaster> toAdd, String sKey)
    {
        for (int i=0; i<toAdd.size();i++)
            this.aAdd(toAdd.get(i).getName(), toAdd.get(i).getServer(), toAdd.get(i).getPath(),
                    toAdd.get(i).getUsername(), toAdd.get(i).getPassword(), toAdd.get(i).getProvider(),
                    toAdd.get(i).getProviderWebSite(), toAdd.get(i).getProviderPhone(),
                    toAdd.get(i).getNote(), sKey);
    }

    public boolean accMod(String name, String server, String path, String userName,
                          String pass, String provider, String providerWebSite,
                          String providerPhone, String note, String sKey)
    {
        for (int i=0; i<this.accSaved.size();i++)
            if (name.equals(this.accSaved.get(i).getName()))
            {
                AccountsMaster aNew = new AccountsMaster();
                aNew.setName(name);
                aNew.setServer(server);
                aNew.setPath(path);
                aNew.setUsername(FunctionsPass.encryptString(sKey, userName));
                aNew.setPassword(FunctionsPass.encryptString(sKey, pass));
                aNew.setProvider(provider);
                aNew.setProviderWebSite(providerWebSite);
                aNew.setProviderPhone(providerPhone);
                aNew.setNote(note);
                this.accSaved.add(i,aNew);
                this.accSaved.remove(i+1);
                return true;
            }
        return false;
    }

    public boolean accDel(String name)
    {
        for (int i=0;i<this.accSaved.size();i++)
            if (name.equals(this.accSaved.get(i).getName()))
            {
                this.accSaved.remove(i);
                return true;
            }
        return false;
    }

    private void aAdd(String name, String server, String path, String userName,
                      String pass, String provider, String providerWebSite,
                      String providerPhone, String note, String sKey)
    {
        AccountsMaster aNew = new AccountsMaster();
        aNew.setName(name);
        aNew.setServer(server);
        aNew.setPath(path);
        aNew.setUsername(FunctionsPass.encryptString(sKey, userName));
        aNew.setPassword(FunctionsPass.encryptString(sKey, pass));
        aNew.setProvider(provider);
        aNew.setProviderWebSite(providerWebSite);
        aNew.setProviderPhone(providerPhone);
        aNew.setNote(note);
        this.accSaved.add(aNew);
    }

    public String getStringToWrite(ArrayList<AccountsMaster> toWrite)
    {
        String strToWrite = Constants.getHeadXmlFile() + Constants.getEndOfLine() + Constants.getHeadOpenXML() + Constants.getAccountRoot() + Constants.getTailXML() + Constants.getEndOfLine();
        if (toWrite!=null)
            for (int i=0; i<toWrite.size(); i++)
            {
                AccountsMaster a = toWrite.get(i);
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountElement() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountName() + Constants.getTailXML() + a.getName() + Constants.getHeadCloseXML() + Constants.getAccountName() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountServer() + Constants.getTailXML() + a.getServer() + Constants.getHeadCloseXML() + Constants.getAccountServer() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountPath() + Constants.getTailXML() + a.getPath() + Constants.getHeadCloseXML() + Constants.getAccountPath() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountUsername() + Constants.getTailXML() + a.getUsername() + Constants.getHeadCloseXML() + Constants.getAccountUsername() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountPassword() + Constants.getTailXML() + a.getPassword() + Constants.getHeadCloseXML() + Constants.getAccountPassword() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountProvider() + Constants.getTailXML() + a.getProvider() + Constants.getHeadCloseXML() + Constants.getAccountProvider() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountProviderWebSite() + Constants.getTailXML() + a.getProviderWebSite() + Constants.getHeadCloseXML() + Constants.getAccountProviderWebSite() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountProviderPhone() + Constants.getTailXML() + a.getProviderPhone() + Constants.getHeadCloseXML()+ Constants.getAccountProviderPhone() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadOpenXML() + Constants.getAccountNote() + Constants.getTailXML() +  a.getNote() + Constants.getHeadCloseXML() + Constants.getAccountNote() + Constants.getTailXML() + Constants.getEndOfLine();
                strToWrite = strToWrite + Constants.getHeadCloseXML() + Constants.getAccountElement() + Constants.getTailXML() + Constants.getEndOfLine();
            }
        strToWrite = strToWrite + Constants.getHeadCloseXML() + Constants.getAccountRoot() + Constants.getTailXML();
        return strToWrite;
    }

    /**
     * Funzione che ricarica da nuovo (eliminando i vecchi item) gli elementi dall'xml
     * @param xml
     */
	public void loadData(String xml)
	{
		try
        {
            Log.w("Accounts","StartParseXML");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            AccountXMLHandler myXMLHandler = new AccountXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(new StringReader(xml)));
            ArrayList<AccountsMaster> itemList = myXMLHandler.getItemsList();
            if (itemList.size()>0)
            {
                this.accSaved = new ArrayList<AccountsMaster>();
                for (int i=0;i<itemList.size();i++)
                    this.accSaved.add(itemList.get(i));

            }
            Log.w("AndroidParseXMLActivity","Done");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
